<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Symfony\Component\VarDumper\VarDumper;

/**
 * Class RabbitMqController
 * @package App\Http\Controllers
 */
class RabbitMqController extends Controller
{
    public function __construct()
    {
    }

    /**
     * 发送消息
     */
    public function sendMessage()
    {
        //配置信息
        $conn_args = array(
            'host' => 'rabbitmq',
            'port' => '5672',
            'vhost' => '/',
            'login' => 'guest',
            'password' => 'guest'
        );
        //路由key
        $route_key = 'queue_name_tang';

        /**
         * 链接设置
         */
        $conn = new \AMQPConnection($conn_args);
        if (!$conn->connect()) {
            die("Cannot connect to the broker!\n");
        }

        /**
         * 管道设置
         */
        $channel = new \AMQPChannel($conn);

        /**
         * 交换机设置
         */
        $exchange_name = 'exchange_name_tang'; //交换机名
        //创建交换机对象
        $exchange = new \AMQPExchange($channel);
        $exchange->setName($exchange_name);
        $exchange->setType(AMQP_EX_TYPE_DIRECT); //direct类型  必须
        $exchange->setFlags(AMQP_DURABLE); //持久化  可选
        $exchange->declareExchange();

        /**
         * 队列设置
         */
        $queue_name = 'queue_name_tang'; //队列名
        //创建队列对象
        $queue = new \AMQPQueue($channel);
        //设置队列名称
        $queue->setName($queue_name);
        //持久化
        $queue->setFlags(AMQP_DURABLE);
        //申报创建队列
        $queue->declareQueue();
        //绑定
        $queue->bind($exchange_name, $route_key);

        /**
         * 发送消息设置
         */
        //开始事务
        $channel->startTransaction();
        //消息内容
        $data = ['id' => 1, 'content' => '测试内容'];
        $result = $exchange->publish(json_encode($data), $route_key);
        $channel->commitTransaction(); //提交事务
        //断开链接
        $conn->disconnect();

        $msg = $result?'发送成功':"发送失败";

        VarDumper::dump($msg);
    }

    /**
     * 接收消息
     */
    public function recMessage()
    {
        //配置信息
        $conn_args = array(
            'host' => 'rabbitmq',
            'port' => '5672',
            'vhost' => '/',
            'login' => 'guest',
            'password' => 'guest'
        );

        $route_key = 'queue_name_tang';//路由key
        $queue_name = 'queue_name_tang'; //队列名
        $exchange_name = 'exchange_name_tang'; //交换机名

        //创建连接和channel
        $conn = new \AMQPConnection($conn_args);
        if (!$conn->connect()) {
            die("Cannot connect to the broker!\n");
        }
        $channel = new \AMQPChannel($conn);

        //创建交换机
        $exchange = new \AMQPExchange($channel);
        $exchange->setName($exchange_name);

        //创建队列
        $queue = new \AMQPQueue($channel);
        $queue->setName($queue_name);

        //绑定交换机与队列，并指定路由键
        $queue->bind($exchange_name, $route_key);

        //接收消息
        $result = $queue->get();
        $msg = '队列中已经没有数据了';
        if ($result) {
            $msg = $result->getBody();
            $envelopeID = $result->getDeliveryTag();
            //确定收到消息，刪除记录
            $queue->ack($envelopeID);
            $msg = json_decode($msg, 1);
        }
        //断开连接
        $conn->disconnect();
        VarDumper::dump($msg);
    }

}
